package week4;

public class week4 {
    public static void main(String[] args) {
    int[] age = {18, 17, 20, 19, 35, 24, 16};

    System.out.println("排序前:");
    printArray(age);

    // Selection Sort
    int n = age.length;
    for (int i = 0; i < n - 1; i++) {
        int s = i;
        for (int j = i + 1; j < n; j++) {
            if (age[s] > age[j]) {
                s = j;
            }
        }
        // Swap
        int r = age[s];
        age[s] = age[i];
        age[i] = r;
    }

    System.out.println("排序后:");
    printArray(age);

    // Bubble Sort
    for (int i = 0; i < age.length - 1; i++) {
        for (int j = 0; j < age.length - 1 - i; j++) {
            if (age[j] > age[j + 1]) {
                // Swap
                int temp = age[j];
                age[j] = age[j + 1];
                age[j + 1] = temp;
            }
        }
    }

    System.out.println("冒泡后:");
    printArray(age);
}

    // Method to print array elements
    private static void printArray(int[] array) {
        for (int value : array) {
            System.out.print(value + " ");
        }
        System.out.println();
    }
}

